package com.ruoyi.fun.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fun.domain.EcCoupon;
import com.ruoyi.fun.service.IEcCouponService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 优惠券Controller
 * 
 * @author ruoyi
 * @date 2025-08-06
 */
@RestController
@RequestMapping("/fun/ecCoupon")
public class EcCouponController extends BaseController
{
    @Autowired
    private IEcCouponService ecCouponService;

    /**
     * 查询优惠券列表
     */
    @PreAuthorize("@ss.hasPermi('fun:coupon:list')")
    @GetMapping("/list")
    public TableDataInfo list(EcCoupon ecCoupon)
    {
        startPage();
        Long orgId = getOrgId();
        // 103组织拥有不过滤的权限
        if (orgId != null && orgId == 103L) {
            orgId = null;
        }
        ecCoupon.setOrgId(orgId);
        List<EcCoupon> list = ecCouponService.selectEcCouponList(ecCoupon);
        return getDataTable(list);
    }

    /**
     * 导出优惠券列表
     */
    @PreAuthorize("@ss.hasPermi('fun:coupon:export')")
    @Log(title = "优惠券", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EcCoupon ecCoupon)
    {
        Long orgId = getOrgId();
        // 103组织拥有不过滤的权限
        if (orgId != null && orgId == 103L) {
            orgId = null;
        }
        ecCoupon.setOrgId(orgId);
        List<EcCoupon> list = ecCouponService.selectEcCouponList(ecCoupon);
        ExcelUtil<EcCoupon> util = new ExcelUtil<EcCoupon>(EcCoupon.class);
        util.exportExcel(response, list, "优惠券数据");
    }

    /**
     * 获取优惠券详细信息
     */
    @PreAuthorize("@ss.hasPermi('fun:coupon:query')")
    @GetMapping(value = "/{couponId}")
    public AjaxResult getInfo(@PathVariable("couponId") Long couponId)
    {
        Long orgId = getOrgId();
        // 103组织拥有不过滤的权限
        if (orgId != null && orgId == 103L) {
            orgId = null;
        }
        return success(ecCouponService.selectEcCouponByCouponIdAndOrgId(couponId, orgId));
    }

    /**
     * 新增优惠券
     */
    @PreAuthorize("@ss.hasPermi('fun:coupon:add')")
    @Log(title = "优惠券", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EcCoupon ecCoupon)
    {
        ecCoupon.setOrgId(getOrgId());
        return toAjax(ecCouponService.insertEcCoupon(ecCoupon));
    }

    /**
     * 修改优惠券
     */
    @PreAuthorize("@ss.hasPermi('fun:coupon:edit')")
    @Log(title = "优惠券", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EcCoupon ecCoupon)
    {
        return toAjax(ecCouponService.updateEcCoupon(ecCoupon));
    }

    /**
     * 删除优惠券
     */
    @PreAuthorize("@ss.hasPermi('fun:coupon:remove')")
    @Log(title = "优惠券", businessType = BusinessType.DELETE)
	@DeleteMapping("/{couponIds}")
    public AjaxResult remove(@PathVariable Long[] couponIds)
    {
        return toAjax(ecCouponService.deleteEcCouponByCouponIds(couponIds));
    }
    /**
     * 根据couponId和组织ID获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('fun:coupon:query')")
    @GetMapping(value = "/{couponId}/org/{orgId}")
    public AjaxResult getInfoByOrgId(@PathVariable("couponId") Long couponId, @PathVariable("orgId") Long orgId)
    {
        return success(ecCouponService.selectEcCouponByCouponIdAndOrgId(couponId, orgId));
    }

    /**
     * 根据组织ID查询列表
     */
    @PreAuthorize("@ss.hasPermi('fun:coupon:list')")
    @GetMapping("/org/{orgId}")
    public TableDataInfo listByOrgId(@PathVariable Long orgId)
    {
        startPage();
        EcCoupon ecCoupon = new EcCoupon();
        List<EcCoupon> list = ecCouponService.selectEcCouponListByOrgId(ecCoupon, orgId);
        return getDataTable(list);
    }


}